package com.ailk.emall.busi.productmanage.controller;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ai.gcf.api.emall.pmloginauth.interfaces.IPmLoginAuthDubboSV;
import com.ai.gcf.api.emall.pmloginauth.param.ModifyPwdRequest;
import com.ai.gcf.api.emall.pmloginauth.param.ModifyPwdResponse;
import com.ai.gcf.api.emall.productmanage.interfaces.IProductManageDealDubboSV;
import com.ai.gcf.api.emall.productmanage.interfaces.IProductManageQueyDubboSV;
import com.ai.gcf.api.emall.productmanage.param.ModifyProductInfoRequest;
import com.ai.gcf.api.emall.productmanage.param.ModifyProductInfoResponse;
import com.ai.gcf.api.emall.productmanage.param.ModifyProductSortRequest;
import com.ai.gcf.api.emall.productmanage.param.ModifyProductSortResponse;
import com.ai.gcf.api.emall.productmanage.param.ModifyProductStatusRequest;
import com.ai.gcf.api.emall.productmanage.param.ModifyProductStatusResponse;
import com.ai.gcf.api.emall.productmanage.param.ProductInfo;
import com.ai.gcf.api.emall.productmanage.param.ProductPageQueryByModuleIdRequest;
import com.ai.gcf.api.emall.productmanage.param.ProductPageQueryByModuleIdResponse;
import com.ai.gcf.api.emall.productmanage.param.SaveProductInfoRequest;
import com.ai.gcf.api.emall.productmanage.param.SaveProductInfoResponse;
import com.ailk.emall.busi.base.controller.BaseController;
import com.ailk.emall.constants.ExcepConstants;
import com.ailk.emall.exception.BusiException;
import com.ailk.emall.utils.MD5Util;
import com.ailk.emall.utils.SessionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/** 
 * @author zhaowg3
 * @Date 2016年12月23日
 */
@Controller
@RequestMapping("/productManage")
public class ProductManageController extends BaseController{
	private Log logger = LogFactory.getLog(ProductManageController.class);
	@Autowired
	private IProductManageQueyDubboSV iProductManageQueyDubboSV;
	@Autowired
	private IProductManageDealDubboSV iProductManageDealDubboSV;
	@Autowired
	private IPmLoginAuthDubboSV iPmLoginAuthDubboSV;
	/**
	 * 页面跳转
	 * @param model
	 * @return
	 * @author zhaowg3
	 * @Date 2016年12月23日
	 */
	@RequestMapping("/goToPage")
	public String toHotProduct(@RequestParam String pagePath){
		return "productManage/"+pagePath;
	}
	
	/**
	 * 查询某个模块下面的产品列表
	 * @return
	 * @author zhaowg3
	 * @throws Exception 
	 * @Date 2016年12月23日
	 */
	@ResponseBody
	@RequestMapping("/products")
	public JSONObject queryProductsByModuleId(@RequestParam String moduleId,@RequestParam String status,@RequestParam int rows,@RequestParam int page) throws Exception{
		//ModuleId:4001-商户|火热爆品-轮播区；4002-商户|活动区；4003-商户|广告区；4004-商户|高返佣专区；4005-商户|精选爆款
		//Status:1:上架 0:下架
		ProductPageQueryByModuleIdRequest request = new ProductPageQueryByModuleIdRequest();
		request.setModuleId(moduleId);
		request.setStatus(status);
		request.setPageNo(page);
		request.setPageSize(rows);
		ProductPageQueryByModuleIdResponse response = iProductManageQueyDubboSV.productPageQueryByModuleId(request);
		if(!response.getResponseHeader().getResultCode().equals(ExcepConstants.Special.SUCCESS))
			throw new BusiException(response.getResponseHeader().getResultCode(), response.getResponseHeader().getResultMessage());
		JSONArray productinfos = new JSONArray();
		for (ProductInfo info : response.getProductInfos().getResult()) {
			JSONObject product = JSONObject.parseObject(JSONObject.toJSONString(info));
			product.put("count", response.getProductInfos().getCount());//冗余字段，为了前台判断是否是最后一条记录
			productinfos.add(product);
		}
		JSONObject result = new JSONObject();
		result.put("total", response.getProductInfos().getCount());
		result.put("rows", productinfos);
		return result;
	}
	/**
	 * 增加商品
	 * @param productInfo
	 * @param picFile
	 * @throws Exception
	 * @author zhaowg3
	 * @Date 2017年1月3日
	 */
	@ResponseBody
	@RequestMapping(value = "/addProduct",method=RequestMethod.POST)
	public JSONObject addProduct(SaveProductInfoRequest productInfo,@RequestParam(required=false) MultipartFile picFile) throws Exception{
		String picFilePath = "";
		if(picFile!=null){
			//TODO 上传图片，获取图片在服务器的路径
			picFilePath = "";
		}
		productInfo.setOperId(SessionUtil.getSysOpId(session));
		productInfo.setProductPictureDir(picFilePath);
		SaveProductInfoResponse response = iProductManageDealDubboSV.saveProductInfo(productInfo );
		if(!response.getResponseHeader().getResultCode().equals(ExcepConstants.Special.SUCCESS))
			throw new BusiException(response.getResponseHeader().getResultCode(), response.getResponseHeader().getResultMessage());
		return responseSuccess("成功", null);
	}
	/**
	 * 编辑商品
	 * @param productInfo
	 * @param picFile
	 * @throws Exception
	 * @author zhaowg3
	 * @Date 2017年1月3日
	 */
	@ResponseBody
	@RequestMapping(value = "/editProduct",method=RequestMethod.POST)
	public JSONObject editProduct(ModifyProductInfoRequest productInfo,@RequestParam(required=false) MultipartFile picFile) throws Exception{
		String picFilePath = "";
		if(picFile!=null){
			//TODO 上传图片，获取图片在服务器的路径
			picFilePath = "";
		}
		productInfo.setOperId(SessionUtil.getSysOpId(session));
		productInfo.setProductPictureDir(picFilePath);
		ModifyProductInfoResponse response = iProductManageDealDubboSV.modifyProductInfo(productInfo);
		if(!response.getResponseHeader().getResultCode().equals(ExcepConstants.Special.SUCCESS))
			throw new BusiException(response.getResponseHeader().getResultCode(), response.getResponseHeader().getResultMessage());
		return responseSuccess("成功", null);
	}
	/**
	 * 修改商品状态，上/下架商品,删除商品等
	 * @param productInfo
	 * @param picFile
	 * @throws Exception
	 * @author zhaowg3
	 * @Date 2017年1月3日
	 */
	@ResponseBody
	@RequestMapping(value = "/modifyProductStatus",method=RequestMethod.POST)
	public JSONObject modifyProductStatus(ModifyProductStatusRequest modifyProductStatusRequest) throws Exception{
		modifyProductStatusRequest.setOperId(SessionUtil.getSysOpId(session));
		ModifyProductStatusResponse response = iProductManageDealDubboSV.ModifyProductStatus(modifyProductStatusRequest);
		if(!response.getResponseHeader().getResultCode().equals(ExcepConstants.Special.SUCCESS))
			throw new BusiException(response.getResponseHeader().getResultCode(), response.getResponseHeader().getResultMessage());
		return responseSuccess("成功", null);
	}
	/**
	 * 修改商品顺序
	 * @param productInfo
	 * @param picFile
	 * @throws Exception
	 * @author zhaowg3
	 * @Date 2017年1月3日
	 */
	@ResponseBody
	@RequestMapping(value = "/moveProductSort",method=RequestMethod.POST)
	public JSONObject moveProductSort(ModifyProductSortRequest modifyProductSortRequest) throws Exception{
		modifyProductSortRequest.setOperId(SessionUtil.getSysOpId(session));
		ModifyProductSortResponse response = iProductManageDealDubboSV.ModifyProductSort(modifyProductSortRequest);
		if(!response.getResponseHeader().getResultCode().equals(ExcepConstants.Special.SUCCESS))
			throw new BusiException(response.getResponseHeader().getResultCode(), response.getResponseHeader().getResultMessage());
		return responseSuccess("成功", null);
	}
	/**
	 * 修改密码
	 * @param oldPwd
	 * @param newPwd
	 * @return
	 * @author zhaowg3
	 * @Date 2017年1月7日
	 */
	@ResponseBody
	@RequestMapping(value="/modifyPwd")
	public JSONObject modifyPwd(@RequestParam String oldPwd,@RequestParam String newPwd){
		try{
			ModifyPwdRequest modifyPwdReq = new ModifyPwdRequest();
			//对新密码进行加密
			modifyPwdReq.setNewPwd(MD5Util.MD5(newPwd));
			//生成6位随机码
			String token = RandomStringUtils.random(6);
			modifyPwdReq.setToken(token);
			//对旧密码进行二次加密
			modifyPwdReq.setOldPwd(MD5Util.MD5(MD5Util.MD5(oldPwd)+token));
			modifyPwdReq.setUserName(SessionUtil.getSysOp(session).getLoginName());
			modifyPwdReq.setOpId(SessionUtil.getSysOpId(session));
			ModifyPwdResponse pwdResponse = iPmLoginAuthDubboSV.modifyPwd(modifyPwdReq);
			if(!pwdResponse.getResponseHeader().getResultCode().equals(ExcepConstants.Special.SUCCESS))
				throw new BusiException(pwdResponse.getResponseHeader().getResultCode(), pwdResponse.getResponseHeader().getResultMessage());
		}catch(Exception e){
			e.printStackTrace();
			return responseFailed(e.getMessage(), null);
		}
		return responseSuccess("成功", null);
	}
}
